<?php
/**
 * 加入我们
 */
namespace app\api\controller;
use app\api\model\MemberDonation as MemberDonationModel;
use app\api\model\CharitableClass as CharitableClassModel;
use app\common\model\Member as MemberModel;
use app\common\common;

class MemberDonation extends Baek
{
    /**
     * 提交关于我们
     */
    function add(){
        $postField = 'cid,name,mailbox,phone,content,type_money,money,images';
        $post = $this->request->only(explode(',',$postField),'post',null);
        //验证是否缺少参数
        if(empty($post['cid'])||empty($post['name'])||empty($post['mailbox'])||empty($post['phone'])||empty($post['content'])||empty($post['images'])) return api_error('','Missing parameter',$this->language);

        //是否通过分类进入
        if($post['cid'] > 0){
            $class = CharitableClassModel::getDetails($post['cid'],$this->language);
            if(empty($class)) return api_error('','Parameter error',$this->language);
        }

        $post['mid'] = $this->mid;
        if(empty($post['money'])) $post['money'] = 0;
        if($post['money'] > 0){
            $wallet = MemberModel::wallet($this->mid);
            $currency = common::key_currency($post['type_money']);  //读取币种
            if($wallet[$currency] < $post['money']) return api_error('','Failed to submit, try again later!',$this->language);
        }

        $id = MemberDonationModel::add($post);

        if(empty($id)) return api_error('','Failed to submit, try again later!',$this->language);
        return api_success('');
    }
}